﻿using System;
using System.Diagnostics;
using System.Reflection;
using FxSuperCore.Utils;

namespace FxSuperCore.Log
{
	public class ConsoleLogPrinter : SimpleLogFormatter, ILogPrint
	{
		private void PrintMsg(LogLevel level, object msg)
		{
			Console.WriteLine($"[{DateTime.Now.ToLocalTime()}][{level.GetEnumDesc()}]{FormatObject(msg)}");
		}

		public void Info(object msg) => PrintMsg(LogLevel.INFO, msg);

		public void Debug(object msg) => PrintMsg(LogLevel.DEBUG, msg);

		public void Warnning(object msg) => PrintMsg(LogLevel.WARNNING, msg);

		public void Error(object msg) => PrintMsg(LogLevel.ERROR, msg);

		public void Trace(object msg)
		{
			StackTrace stackTrace = new StackTrace();
			StackFrame[] stackFrames = stackTrace.GetFrames();
			PrintMsg(LogLevel.TRAE, msg);
			int deep = 0;
			if (stackFrames != null)
			{
				foreach (StackFrame stackFrame in stackFrames)
				{
					deep += 1;
					if (deep == 1)
					{
						continue;
					}
					MethodBase method = stackFrame.GetMethod();
					string log = $"{method.DeclaringType.FullName}.{method.Name}";
					if (deep >= 10)
					{
						log += " ...";
						PrintMsg(LogLevel.TRAE, $"{log}...");
						break;
					}

					PrintMsg(LogLevel.TRAE, log);
				}
			}
		}
	}
}
